/*
 * Copyright (C) 2009 Dimitrios C. Michalakos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.restsql.db.io;


import com.restsql.db.ColumnInfo;
import com.restsql.db.Database;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * MySQLDataIO can be used to manipulate the data of a MySQL database.
 * @author dimitris@jmike.gr
 */
public class MySQLDataIO implements DataIO {

    private Database db;

    /**
     * Construct a new MySQLDataIO.
     * @param db the parent database.
     */
    public MySQLDataIO(Database db) {
        this.db = db;
    }

    /**
     * Set the designated parameter for the specified PreparedStatement.
     * @param ps the PreparedStatement.
     * @param index the parameter's index.
     * @param table the name of the parent table of the parameter.
     * @param column the column name of the parameter.
     * @param value the value of the parameter.
     * @throws java.sql.SQLException
     */
    public void setParameter(PreparedStatement ps, int index, String table, String column, String value) throws SQLException {
        ColumnInfo columnInfo = db.getColumnInfo(table);
        System.out.println(table + "." + column + " (" + columnInfo.getDatatype(column).toString() + ")");
        switch (columnInfo.getDatatype(column)) {
            case BIGINT: {
                /* coming soon */
                break;
            }
            case BINARY: {
                /* coming soon */
                break;
            }
            case BIT: {
                /* coming soon */
                break;
            }
            case BLOB: {
                /* coming soon */
                break;
            }
            case CHAR: {
                ps.setString(index, value);
                break;
            }
            case DATE: {
                /* coming soon */
                break;
            }
            case DATETIME: {
                /* coming soon */
                break;
            }
            case DECIMAL: {
                /* coming soon */
                break;
            }
            case DOUBLE: {
                /* coming soon */
                break;
            }
            case ENUM: {
                ps.setString(index, value);
                break;
            }
            case FLOAT: {
                /* coming soon */
                break;
            }
            case INT: {
                if (columnInfo.isSigned(column)) {//Signed
                    final int x = Integer.parseInt(value);
                    ps.setInt(index, x);
                } else {//Unsigned
                    final long x = Long.parseLong(value);
                    ps.setLong(index, x);
                }
                break;
            }
            case LONGBLOB: {
                /* coming soon */
                break;
            }
            case LONGTEXT: {
                ps.setString(index, value);
                break;
            }
            case MEDIUMBLOB: {
                /* coming soon */
                break;
            }
            case MEDIUMINT: {
                final int x = Integer.parseInt(value);
                ps.setInt(index, x);
                break;
            }
            case MEDIUMTEXT: {
                ps.setString(index, value);
                break;
            }
            case SET: {
                ps.setString(index, value);
                break;
            }
            case SMALLINT: {
                final int x = Integer.parseInt(value);
                ps.setInt(index, x);
                break;
            }
            case TEXT: {
                ps.setString(index, value);
                break;
            }
            case TIME: {
                /* coming soon */
                break;
            }
            case TIMESTAMP: {
                /* coming soon */
                break;
            }
            case TINYBLOB: {
                /* coming soon */
                break;
            }
            case TINYINT: {
                /* coming soon */
                break;
            }
            case TINYTEXT: {
                ps.setString(index, value);
                break;
            }
            case VARBINARY: {
                /* coming soon */
                break;
            }
            case VARCHAR: {
                ps.setString(index, value);
                break;
            }
            case YEAR: {
                /* coming soon */
                break;
            }
            default: {
                ps.setString(index, value);
                break;
            }
        }
    }
}
